#include "alp_global_vars.h"
#include	"config_parser.h"
double latency[1000000];
//------------------------------------------------------------------------------
void acceptor_0_deliver(int instance_number, int VALUE) {
	if (!data[(instance_number % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].deliver && data[(instance_number % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].received) {
		data[(instance_number % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].dec = VALUE;
		data[(instance_number % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].deliver = true;
	}
}
void learner_deliver() {
	int i;
	int ld = lowestDelivered;

	for (i = ld + 1; i < alp_interval_performance->highest_deliverable_inst_id + 1; i++) {
		if (data[(i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].received && data[(i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].packet.pack_id == (i + 1)) {
			if ((get_config_info()->self_acceptor_id != get_config_info()->total_num_acceptors - 1) && !data[(i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].deliver) {

				if (data[(i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].packet.p_msgs[0].proposer_id == get_config_info()->self_proposer_id) {
					double l = getMiliSeconds() - data[(i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].packet.p_msgs[0].mili_seconds;
					alp_interval_performance->interval_latency += l;

					if (alp_interval_performance->total_time > DISCARD && alp_interval_performance->total_time < STOPPING_CONDITION * 1000 - DISCARD) {
						latency[alp_interval_performance->latency_counter] = l;
						alp_interval_performance->latency_counter++;
						alp_interval_performance->latency_sum += l;
						alp_interval_performance->latency_sq_sum += pow(l, 2);
					}

					alp_interval_performance->num_packets_delivered_latency++;
					alp_interval_performance->highest_self_deliverable = data[i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers)].packet.p_msgs[0].location_index_in_proposer;
				}
				alp_interval_performance->interval_num_packets_delivered++;

				data[(i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].dec = data[(i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].packet.pack_id;
				data[(i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].deliver = true;
				data[(i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].received = false;
				data[(i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].deliver = false;

				paxos_instances[(i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].vval = data[(i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].packet.pack_id;

				lowestDelivered = i;

				if (get_config_info()->self_acceptor_id == 0) {
					paxos_instances[(i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].vval = data[(i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].packet.pack_id;
				}
				//--------------------------------------------
				//				if (!COORDINATOR) {
				//					paxos_instances[(i % INTERNAL_BUFFER_SIZE)].rnd = 0;
				//					paxos_instances[(i % INTERNAL_BUFFER_SIZE)].vrnd = 0;
				//					paxos_instances[(i % INTERNAL_BUFFER_SIZE)].vval = 0;
				//					paxos_instances[(i % INTERNAL_BUFFER_SIZE)].crnd = -1;
				//					paxos_instances[(i % INTERNAL_BUFFER_SIZE)].cval = -1;
				//				} else {
				//					paxos_instances[(i % INTERNAL_BUFFER_SIZE)].rnd = 0;
				//					paxos_instances[(i % INTERNAL_BUFFER_SIZE)].vrnd = 0;
				//					paxos_instances[(i % INTERNAL_BUFFER_SIZE)].vval = 0;
				//					paxos_instances[(i % INTERNAL_BUFFER_SIZE)].crnd = 1;// before =0
				//					paxos_instances[(i % INTERNAL_BUFFER_SIZE)].cval = 0;
				//				}
			} else if (get_config_info()->self_acceptor_id == get_config_info()->total_num_acceptors - 1) {

				if (data[(i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].packet.p_msgs[0].proposer_id == get_config_info()->self_proposer_id) {
					double l = getMiliSeconds() - data[(i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].packet.p_msgs[0].mili_seconds;
					alp_interval_performance->interval_latency += l;

					if (alp_interval_performance->total_time > DISCARD && alp_interval_performance->total_time < STOPPING_CONDITION * 1000 - DISCARD) {
						latency[alp_interval_performance->latency_counter] = l;
						alp_interval_performance->latency_counter++;
						alp_interval_performance->latency_sum += l;
						alp_interval_performance->latency_sq_sum += pow(l, 2);
					}

					alp_interval_performance->num_packets_delivered_latency++;
					alp_interval_performance->highest_self_deliverable = data[i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers)].packet.p_msgs[0].location_index_in_proposer;
				}
				alp_interval_performance->interval_num_packets_delivered++;
				data[(i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].dec = data[(i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].packet.pack_id;
				data[(i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].received = false;
				data[(i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].deliver = false;

				paxos_instances[(i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].vval = data[(i % (INTERNAL_BUFFER_SIZE * get_config_info()->total_num_proposers))].packet.pack_id;

				//---------------------------------------------------
				//				paxos_instances[(i % INTERNAL_BUFFER_SIZE)].rnd = 0;
				//				paxos_instances[(i % INTERNAL_BUFFER_SIZE)].vrnd = 0;
				//				paxos_instances[(i % INTERNAL_BUFFER_SIZE)].vval = 0;
				//				paxos_instances[(i % INTERNAL_BUFFER_SIZE)].crnd = -1;
				//				paxos_instances[(i % INTERNAL_BUFFER_SIZE)].cval = -1;
				lowestDelivered = i;
			}
		} else
			break;
	}
}
//------------------------------------------------------------------------------
